import serial

if __name__ == '__main__':
    serial_port = serial.Serial('COM6', 115200, timeout=5)  ##连接串口，打开
    file_path = 'C:\\Users\\T480S\\Desktop\\IotSolution\\2-Embed\\MicroBootloader\\stm32f103c8\\AppLed\\OBJ\\AppLed.bin'
    file_path = 'C:\\Users\\latefly\\Desktop\\IotSolution\\2-Embed\\MicroBootloader\\stm32f103c8\\AppLed\\OBJ\\AppLed.bin'
    file = open(file_path, 'rb')
    data = file.read()

    index = 0
    data_len = 255
    while (index < len(data)):
        # 按照TLV的长度分割数据
        serial_port.write(data[index:index + data_len])
        index += data_len
    serial_port.write(data[index - data_len:len(data)])

    while True:
        # Write = serial_port.write(b'Hello\n')  ##发送数据
        data = serial_port.read_all()  ###接收1个字节数据
        if len(data) > 0:
            print(str(data))

    # Write = serial_port.write(b'Hello\n')  ##发送数据
    # Read = serial_port.readline()  ##接收一行数据
    # print(Read)
    #
    # Write = serial_port.write(b'Hello\n')  ##发送数据
    # Read = serial_port.read_all()  ###接收所有数据
    # print(Read)
    #
    # Write = serial_port.write(b'Hello\n')  ##发送数据
    # Read = serial_port.readlines()  ###读多行数据
    # print(Read)

    serial_port.close()  ###关闭串口连接
